#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int T,n,a[N][2];
long long C=0;
int main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    scanf("%d",&T);
    while(T--)
    {
        memset(a,0,sizeof(a));C=0;
        scanf("%d",&n);//a[x][1]=1:red =2:blue
        for(int i=1;i<=n;i++)scanf("%d",&a[i][0]);
        int ans=0,mx=-1;
        a[1][1]=1;
        for(int i=2;i<=n;i++)
        {
            if(a[i][0]==a[i-1][0])a[i][1]=a[i-1][1],C+=1LL*a[i][0];
            else
            {
                int f=0,col=a[i-1][1],pos=-1;
                for(int j=i-1;j>0;j--)if(a[j][0]==a[i][0]){f=1,pos=j,col=a[j][1];break;}
                if(!f)a[i][1]=col;
                else if(a[i][0]==a[pos][0])C+=1LL*a[pos][0],a[i][1]=a[pos][1];
                else a[i][1]=a[pos][1];
            }
        }
        printf("%lld\n",C);
    }
    return 0;
}
//It is an exercise of DP,but it seems I can't finish it successfully...Just do it then^v^
//Fine then,I can't make it!TAT......
//Why cannot you give us a simple question?I only want to get into NOIp......
//Who had I bothered?
//example 2
//Release 20.03  rev 11997 (2020-03-28 13:56:24)   gcc 9.3.0 Linux/unicode - 64 bit
//Release 20.03  rev 11997 (2020-03-28 13:56:24)   gcc 9.3.0 Linux/unicode - 64 bit

//It is an exercise of DP,obviously,but it seems I can't finish it successfully...Just do it then^v^
//Fine then,I can't make it!TAT......
//Why cannot you give us a simple question?I only want to get into NOIp......
//Who had I bothered?Huh???!!!
//example 2
//Release 20.03  rev 11997 (2020-03-28 13:56:24)   gcc 9.3.0 Linux/unicode - 64 bit
//It is an exercise of DP,but it seems I can't finish it successfully...Just do it then^v^
//Fine then,I can't make it!TAT......
//Why cannot you give us a simple question?I only want to get into NOIp......
//Who had I bothered?
//example 2
//Release 20.03  rev 11997 (2020-03-28 13:56:24)   gcc 9.3.0 Linux/unicode - 64 bit
//Release 20.03  rev 11997 (2020-03-28 13:56:24)   gcc 9.3.0 Linux/unicode - 64 bit

